<template>
  <div>
    <van-uploader :after-read="afterRead" />
    <img :src="url" alt="" />
    <van-field v-model="mobile" label="手机号" placeholder="请输入手机号" />
    <van-field v-model="pwd" label="密码" placeholder="请输入密码" />
    <van-button @click="dl">登录/注册</van-button>
  </div>
</template>

<script>


export default {
  data() {
    return {
      url: "",
      token: "",
      pwd: "",
      mobile: "",
    };
  },
  mounted() {
    this.gettoken();
  },
  methods: {
    dl(){
      var data = {"mobile":this.mobile,"pwd":this.pwd}
      this.axios.post("/login",data).then(res =>{
        if(res.data.code == 200){
          alert(res.data.msg)
          localStorage.setItem("mobile",res.data.mobile)
          localStorage.setItem("userid",res.data.userid)
          localStorage.setItem("roleid",res.data.roleid)
          localStorage.setItem("token",res.data.token)
          localStorage.setItem("menulist",JSON.stringify(res.data.data))
          localStorage.setItem("myurl",JSON.stringify(res.data.myurl))
          this.$router.push("/about")
        }else{
          alert(res.data.msg)
        }
      })
    },
    bdapi() {
      this.axios.get("bdapi?url=" + this.url).then((res) => {
        this.mobile = res.data.mobile;
        this.pwd = res.data.pwd;
      });
    },
    // 获取七牛token
    gettoken() {
      this.axios.post("/qntoken").then((res) => {
        this.token = res.data.token;
      });
    },
    // 图片存储在七牛云
    afterRead(file) {
      // 此时可以自行将文件上传至服务器
      console.log(file);
      var data = new FormData();
      data.append("token", this.token);
      data.append("file", file.file);
      this.axios
        .post("https://upload-cn-east-2.qiniup.com", data)
        .then((res) => {
          this.url = "http://rrk5b8jnj.bkt.clouddn.com/" + res.data.key;
          // 调用百度api
          this.bdapi();
        });
    },
  },
};
</script>

<style>
</style>